package com.yztc.love_travel.adater;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.love_travel.R;
import com.yztc.love_travel.bean.homepage.BaseBean;
import com.yztc.love_travel.bean.homepage.FourTripBean;
import com.yztc.love_travel.bean.homepage.MainServiceBean;
import com.yztc.love_travel.bean.homepage.RecommendTripNotesBean;
import com.yztc.love_travel.ui.mainservice.buy.view.BuyActivity;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.mainservice.eating.view.EatingActivity;
import com.yztc.love_travel.ui.mainservice.hotel.view.HotelActivity;
import com.yztc.love_travel.ui.mainservice.historyculture.view.HistoryCultureActivity;
import com.yztc.love_travel.ui.mainservice.playinlocal.view.PlayInLocalActivity;
import com.yztc.love_travel.ui.mainservice.scenerystrategy.view.SceneryStrategyActivity;

import com.yztc.love_travel.ui.fourtrip.GuLangYuCultureActivity;
import com.yztc.love_travel.ui.fourtrip.ReligionCultureActivity;
import com.yztc.love_travel.ui.fourtrip.RomanticActivity;
import com.yztc.love_travel.ui.fourtrip.SmallStoryActivity;

import com.yztc.love_travel.ui.recommondtripnotes.view.ArticleDetailsActivity;

import java.util.List;

/**
 * Created by 周旭 on 2016/11/28.
 * "主页中的RecyclerView的适配器"
 */

public class MainAdapter extends RecyclerView.Adapter{

    private static final int TYPE_DEFAULT = 0; //默认的
    private static final int TYPE_MAIN_SERVICE = 1; //首页的主要服务
    private static final int TYPE_FOUR_TRIP = 2; //首页的四大旅行推荐
    private static final int TYPE_NEXT_STOP = 3;//发现下一站
    private static final int TYPE_RECOMMEND_TRIP_NOTES = 4; //推荐游记

    private List<BaseBean> mList;
    private LayoutInflater inflater;
    private Context context;

    public MainAdapter(Context context,List<BaseBean> mList) {
        this.mList = mList;
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
         super.getItemViewType(position);
        int type = mList.get(position).getType();
        switch (type){
            case 1:
                return TYPE_MAIN_SERVICE;
            case 2:
                return TYPE_FOUR_TRIP;
            case 3:
                return TYPE_NEXT_STOP;
            case 4:
                return TYPE_RECOMMEND_TRIP_NOTES;
            default:
                return TYPE_DEFAULT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        switch (viewType){
            case TYPE_MAIN_SERVICE:
                itemView = inflater.inflate(R.layout.main_service,parent,false);
                return new MainServeViewHolder(itemView);
            case TYPE_FOUR_TRIP:
                itemView = inflater.inflate(R.layout.main_four_trip,parent,false);
                return new FourTripViewHolder(itemView);
            case TYPE_NEXT_STOP:
                itemView = inflater.inflate(R.layout.main_find_next_stop,parent,false);
                return new NextStopViewHolder(itemView);
            case TYPE_RECOMMEND_TRIP_NOTES:
                itemView = inflater.inflate(R.layout.main_recommend_trip_notes,parent,false);
                return new RecommendTripViewHolder(itemView);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if(holder instanceof MainServeViewHolder){
            final MainServiceBean mainServiceBean = (MainServiceBean) mList.get(position);
            ((MainServeViewHolder) holder).headerIv.setImageResource(mainServiceBean.getPic_id());
            ((MainServeViewHolder) holder).titleTv.setText(mainServiceBean.getTitle());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击Item，跳转到对应的页面
                    int pic_id = mainServiceBean.getPic_id();
                    Intent intent = new Intent();
                    switch (pic_id){
                        case R.mipmap.home_lishiwenhua:
                            //跳转到"历史文化"界面
                            intent.setClass(context, HistoryCultureActivity.class);
                            break;
                        case R.mipmap.home_jingdiangonglue:
                            //跳转到"景点攻略"界面
                            intent.setClass(context, SceneryStrategyActivity.class);
                            break;
                        case R.mipmap.home_wdd:
                            //跳转到"玩当地"界面
                            intent.setClass(context, PlayInLocalActivity.class);
                            break;
                        case R.mipmap.home_cms:
                            //跳转到"吃美食"界面
                            intent.setClass(context, EatingActivity.class);
                            break;
                        case R.mipmap.home_zjd:
                            //跳转到"住酒店"界面
                            intent.setClass(context, HotelActivity.class);
                            break;
                        case R.mipmap.home_hlg:
                            //跳转到"欢乐购"界面
                            intent.setClass(context, BuyActivity.class);
                            break;
                    }
                    context.startActivity(intent);
                }
            });
        }else if(holder instanceof FourTripViewHolder){
            setFourTripData((FourTripViewHolder) holder, position);
        }else if(holder instanceof NextStopViewHolder){

        }else if(holder instanceof RecommendTripViewHolder){
            setRecommendTripNotes((RecommendTripViewHolder) holder, position);
        }
    }

    //设置4个推荐的旅游路线的数据
    private void setFourTripData(FourTripViewHolder holder, int position) {
        final FourTripBean fourTripBean = (FourTripBean) mList.get(position);
        holder.headerIv.setImageResource(fourTripBean.getPic_id());
        holder.titleTv.setText(fourTripBean.getTitle());

        holder.headerIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                switch (fourTripBean.getPic_id()){
                    case R.drawable.religion_culture:
                        // //跳转到"宗教文化心灵之旅'页面
                        intent.setClass(context, ReligionCultureActivity.class);
                        break;
                    case R.drawable.culture_trip:
                        //跳转到"鼓浪屿文化之旅'页面
                        intent.setClass(context, GuLangYuCultureActivity.class);
                        break;
                    case R.drawable.romantic:
                        //跳转到"滨海环岛浪漫之旅'页面
                        intent.setClass(context, RomanticActivity.class);
                        break;
                    case R.drawable.story_trip:
                        //跳转到"小城故事街巷之旅'页面
                        intent.setClass(context, SmallStoryActivity.class);
                        break;
                }
                context.startActivity(intent);
            }
        });
    }

    //设置"推荐游记"的数据
    private void setRecommendTripNotes(final RecommendTripViewHolder holder, int position) {
        final RecommendTripNotesBean.ManualArticleListBean manualArticleListBean  = (RecommendTripNotesBean.ManualArticleListBean) mList.get(position);
        //头图片的完整地址
        String headerPicUrl = URLConstant.RecommendNotes.URL_RECOMMEND_NOTES_ITEM_PIC + manualArticleListBean.getCoverFoto();
        Glide.with(context).load(headerPicUrl)
                .placeholder(R.mipmap.logo)
                .error(R.mipmap.logo)
                .into(holder.headerIv); //加载头图片
        holder.titleTv.setText(manualArticleListBean.getTitle()); //设置标题
        holder.contentTv.setText(manualArticleListBean.getContent()); //设置简介内容
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击Item，跳转到详情页面
                int articleId = manualArticleListBean.getArticleId(); //用于拼接详情页面的文章id
                Intent intent = new Intent(context, ArticleDetailsActivity.class);
                intent.putExtra(OtherConstant.KEY_ARTICAL_ID,articleId);
                context.startActivity(intent);
            }
        });
    }

    private class MainServeViewHolder extends RecyclerView.ViewHolder{

        private ImageView headerIv;
        private TextView titleTv;

        public MainServeViewHolder(View itemView) {
            super(itemView);
            headerIv = (ImageView) itemView.findViewById(R.id.main_service_header_iv);
            titleTv = (TextView) itemView.findViewById(R.id.main_service_title_tv);
        }
    }

    private class FourTripViewHolder extends RecyclerView.ViewHolder{
        private ImageView headerIv;
        private TextView titleTv;

        public FourTripViewHolder(View itemView) {
            super(itemView);

            headerIv = (ImageView) itemView.findViewById(R.id.four_trip_header_iv);
            titleTv = (TextView) itemView.findViewById(R.id.four_trip_title_tv);
        }
    }

    private class NextStopViewHolder extends RecyclerView.ViewHolder{

        public NextStopViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class RecommendTripViewHolder extends RecyclerView.ViewHolder{

        private ImageView headerIv; //头图片
        private TextView titleTv; //标题
        private TextView contentTv;//内容

        public RecommendTripViewHolder(View itemView) {
            super(itemView);

            headerIv = (ImageView) itemView.findViewById(R.id.trip_notes_header_iv);
            titleTv = (TextView) itemView.findViewById(R.id.trip_notes_title_tv);
            contentTv = (TextView) itemView.findViewById(R.id.trip_notes_content_tv);
        }
    }
}
